package top.cn27.luckyhe.mirai.plugin.residence.utils;

import net.kronos.rkon.core.Rcon;
import net.kronos.rkon.core.ex.AuthenticationException;

import java.io.IOException;
import java.util.Map;

public class RconUtil {
    private static String host;
    private static int port;
    private static String password;

    public static String sendMcCommand(String command) throws IOException, AuthenticationException {
        getConnInfo();

        Rcon rcon = new Rcon(host, port, password.getBytes());
        String res = rcon.command(command);
        rcon.disconnect();

        return McCommandResultReplace.removeStrangeChar(res);
    }

    private static void getConnInfo() throws IOException {
        if (host == null && port == 0 && password == null) {
            Map<String, String> map = ConfigOperation.getRconConnInfo();
            host = map.get("host");
            port = Integer.parseInt(map.get("port"));
            password = map.get("password");
        }
    }
}